package com.atguigu.realtime.publisher.trade.controller;

import com.atguigu.realtime.publisher.trade.bean.TradeProvinceOrderBean;
import com.atguigu.realtime.publisher.trade.service.TradeProvinceOrderService;
import com.atguigu.realtime.publisher.util.GetTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/trade")
public class TradeProvinceOrderController {

    @Autowired
    TradeProvinceOrderService tradeProvinceOrderService;

    @RequestMapping("/provinceTrade")
    public String getProvinceOrderInfo(@RequestParam(value = "date", defaultValue = "1", required = false) String date){
        if ("1".equals(date)){
            date = GetTimeUtil.getToday();
        }

        List<TradeProvinceOrderBean> provinceOrderList = tradeProvinceOrderService.getProvinceOrder(date);

        if (provinceOrderList==null) return "";

        String data = provinceOrderList.stream().map(
                bean -> "{\"name\": \"" + bean.getProvince() + "\"," +
                        " \"value\": " + bean.getProvinceId()+", " +
                        "\"tooltipValues\": [\n" +
                        bean.getOrderCount() + " , " + bean.getOrderAmount() +
                        "        ]"
                        + "}"
        ).collect(
                Collectors.joining(",")
        );



        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\": [\n" +
                data +
                "    ] ," +
                " \"valueName\": \"省份ID\"," +
                "   \"tooltipNames\": [\n" +
                "      \"订单数\",\n" +
                "      \"订单总额\"\n" +
                "    ]" +
                "  }\n" +
                "}" ;
    }

    @RequestMapping("/total")
    public String getTotalAmount(@RequestParam(value = "date", defaultValue = "1", required = false) String date){
        if ("1".equals(date)){
            date = GetTimeUtil.getToday();
        }

        BigDecimal totalAmount = tradeProvinceOrderService.getTotalAmount(date);

        if (totalAmount == null) totalAmount = BigDecimal.ZERO;

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+totalAmount+"\n" +
                "}";
    }
}
